﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using OnLineExam.Data;
using HHG.Common;

namespace OnLineExam.Web.mag.Handler
{
    /// <summary>
    /// SingleHandler 的摘要说明
    /// </summary>
    public class SingleHandler : IHttpHandler
    {
		private OnlineExamEntities _dbContext;
		private EfRepository<SingleProblem> _singleRepository;
        public SingleHandler()
		{
			_dbContext = new OnlineExamEntities();
			_dbContext.Configuration.LazyLoadingEnabled = false; 
			_singleRepository = new EfRepository<SingleProblem>(_dbContext);
        }


        public void ProcessRequest(HttpContext context)
        {
            
            _singleRepository = new EfRepository<SingleProblem>(_dbContext);
            string type = context.Request.Params["type"];
            var paperId =  context.Request.Params["id"].GetInt();
            switch (type)
            {
                case "getlist":
                    #region 获取列表
                    int page = int.Parse(context.Request.Params["page"]);
                    int rows = int.Parse(context.Request.Params["rows"]);

                    var query =_singleRepository.TableNoTracking;
                    if (paperId > 0)
                        query = query.Where(s => s.PaperID == paperId);

                   

                    int RecordCount = query.Count();
                    var items = query.OrderBy(s=>s.ID).Skip((page - 1) * rows).Take(rows).Select(s=>new { ID=s.ID,Title=s.Title, });

					  
                    context.Response.Write(new  DataGrid(RecordCount,items).SerializeObject());

                    #endregion
                    break;
                case "edit":
                    #region 根据ID获取信息
                    var ID = context.Request.Params["ID"].GetInt();
                    if (ID>0)
                    {
						var entity = _singleRepository.GetById(ID);
						context.Response.Write(entity.SerializeObject());

					}

                    #endregion
                    break;
                case "addsave":
					#region 提交修改
					var addPaperId = context.Request.Params["selectPaper"].GetInt();

					var new_model = new OnLineExam.Data.SingleProblem();
					new_model.PaperID = addPaperId;

					 
					new_model.Title = context.Request.Params["txtTitle"];
					new_model.AnswerA = context.Request.Params["txtA"];
					new_model.AnswerB = context.Request.Params["txtB"];
					new_model.AnswerC = context.Request.Params["txtC"];
					new_model.AnswerD = context.Request.Params["txtD"];
					new_model.Answer = context.Request.Params["selectAnswer"];

					 _singleRepository.Insert(new_model);
					context.Response.Write("1");//修改成功，返回ID.失败，返回0

					#endregion
					break;
				case "editsave":
					#region 提交修改
					 
					var  editID = context.Request.Params["hdID"].GetInt();
				 
					new_model = _singleRepository.GetById(editID);
					new_model.ID = editID;
					 
					//new_model.CourseID = int.Parse(context.Request.Params["selectCourse"]);
					new_model.PaperID = int.Parse(context.Request.Params["selectPaper"]);
					new_model.Title = context.Request.Params["txtTitle"];
					new_model.AnswerA = context.Request.Params["txtA"];
					new_model.AnswerB = context.Request.Params["txtB"];
					new_model.AnswerC = context.Request.Params["txtC"];
					new_model.AnswerD = context.Request.Params["txtD"];
					new_model.Answer = context.Request.Params["selectAnswer"];

					_singleRepository.Update(new_model);
					context.Response.Write(editID);//修改成功，返回ID
					#endregion
					break;
				case "del":
					#region 删除
					var deleteID = context.Request.Params["id"].GetInt();
					_singleRepository.Delete(_singleRepository.GetById(deleteID));
					context.Response.Write(deleteID);//删除成功，返回ID
					#endregion
					break;
				default:
					break;
			}
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}